import { baseRequestClient, requestClient } from '#/api/request';

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    loginName: string;
    password: string;
  }

  /** 注册接口参数 */
  export interface RegisterParams {
    username: string;
    password: string;
    phone: string;
  }

  /** 权限菜单项接口 */
  export interface PermissionItem {
    id: number;
    menuName: string;
    path: string;
    component: string;
    status: string;
    perms: string;
    parentId: number | null;
    children: PermissionItem[] | null;
  }

  /** 用户角色接口 */
  export interface UserRole {
    id: number;
    name: string;
    roleKey: string;
    status: number;
  }

  /** 用户信息接口 */
  export interface UserData {
      id: number;
      username: string;
      password: string;
    status: number | null;
      realName: string | null;
      idCard: string | null;
      idCardFrontUrl: string | null;
      idCardBackUrl: string | null;
      nickName: string | null;
      gender: number;
      birthday: string | null;
      avatar: string | null;
      phone: string;
    phoneVerified: number | null;
      lastLoginAt: string | null;
    loginCount: number | null;
    verificationStatus: number | null;
      openId: string | null;
    token: string | null;
    role: UserRole;
    perms: PermissionItem[];
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    code: number;
    message: string;
    data: UserData;
  }

  /** 获取用户信息返回值 */
  export interface GetUserInfoResult {
    code: number;
    message: string;
    data: UserData;
  }

  /** 注册接口返回值 */
  export interface RegisterResult {
    code: number;
    message: string;
    data: {
      id: number;
      username: string;
      password: string;
      status: number | null;
      realName: string | null;
      idCard: string | null;
      idCardFrontUrl: string | null;
      idCardBackUrl: string | null;
      nickName: string | null;
      gender: number | null;
      birthday: string | null;
      avatar: string | null;
      phone: string;
      phoneVerified: number | null;
      lastLoginAt: string | null;
      loginCount: number | null;
      verificationStatus: number | null;
      openId: string | null;
      token: string;
      role: any;
      perms: string[];
    };
  }

  export interface RefreshTokenResult {
    data: string;
    status: number;
  }
}

/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  return requestClient.post<AuthApi.UserData>('/user/backLogin', data);
}

/**
 * 注册
 */
export async function registerApi(data: AuthApi.RegisterParams) {
  return requestClient.post<AuthApi.RegisterResult['data']>('/user/backRegister', data);
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi() {
  return baseRequestClient.post<AuthApi.RefreshTokenResult>('/auth/refresh', {
    withCredentials: true,
  });
}

/**
 * 获取用户信息
 */
export async function getUserInfoApi(userId: number | string) {
  return requestClient.get<AuthApi.UserData>(`/user/${userId}`);
}

/**
 * 退出登录
 */
export async function logoutApi() {
  return baseRequestClient.post('/auth/logout', {
    withCredentials: true,
  });
}
